#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e7+7;
const int INF = 0x3f3f3f3f;
int a[MAXN]; //数组
int cha[MAXN];
LL n, m, k, p, q, r, MOD;
int Next(LL ask, LL i) {
	return (p*ask + q*i + r) % MOD;
}
struct node {
	int i; //位置
	int value;
	node(int ii, int vvalue) {i=ii; value=vvalue; }
	node() {}
	bool operator < (const node& a) const {
		return value > a.value;
	}
};
int main() {
#ifdef hai
    freopen("in.txt", "r", stdin);
#endif
	int T;
	cin>>T;
	while (T--) {
		scanf("%lld %lld %lld %lld %lld %lld %lld", &n, &m, &k, &p, &q, &r, &MOD);
		LL t;
		for (int i=1; i<=k; ++i) {
			scanf("%d", a+i);
		}
		for (int i=k+1; i<=n; ++i) {
			a[i] = Next(a[i-1], i);
		}
		priority_queue<node> pq;
		for (int i=1; i<=n; ++i) {
			int t = a[i];
			while (!pq.empty() && pq.top().value < t) {
				cha[pq.top().i] = i;
				pq.pop();
			}
			pq.push(node(i, t));
		}
		while (!pq.empty()) {
			cha[pq.top().i] = INF;
			pq.pop();
		}
		queue<int> q;
		multiset<int> mset;
		int cnt = 0;
		for (int i=1, t=0; i<=m; ++i) {
			q.push(a[i]);
			mset.insert(a[i]);
			if (a[i]>t) {
				t=a[i];
				++cnt;
			}
		}
		int len = n-m+1;
		LL A = 0, B = 0;
		for (int i=1, t; i<=len; ++i) {
//			printf("--maxrating, cnt--\n");
//			printf("%lld %lld\n", *(--mset.end()), cnt);
//			printf("--maxrating, cnt--\n");
			t = *(--mset.end()) ^ i;
			A = A + t;
			t = cnt ^ i;
//			printf("--cnt^i: %d\n", t);
			B = B + t;

			if (i==len) break;
			//滚动到下一个
            assert(!q.empty()); //判断队列是否为空
			t = q.front(); q.pop();//可能存在溢出
			int mdzz = *(--mset.end());
			mset.erase(t);
			t = a[i+m];
			q.push(t);
			mset.insert(t);
//			printf("mdzz: %d\n", mdzz);
			if (t > mdzz) ++cnt;
			t=cha[i];
//			printf("cha[%d]: %d\n", i, cha[i]);
			if (t==i+1) {
//				printf(" -- haha--\n");
				--cnt;
				continue;
			}
			int mar = cha[i+1];
//			printf("mar: %d\n", mar);
			while (mar != t && mar < i+m && mar < MAXN && mar!=0) {
				++cnt;
//				printf("here\n");
				mar = cha[mar];
			}
		}
		printf("%lld %lld\n", A, B);
	}
	return 0;
}
